$(function(){
	
	$.post("proCategory.action",function(data){
    	$("#s_category").append(data);
    });
	$.post("product.action",function(data){
    	$("#item").append(data);
    	$("#e_item").append(data);
    });
	$("#price,#number,#e_price,#e_number,#table4 tr td input:not([id='s_item'])").numeric({allow:"."});
	$("#price,#number").poshytip({
		content: 'Enter a number',
		className: 'tip-yellowsimple',
		showOn: 'focus',
		alignTo: 'target',
		alignX: 'inner-left',
		offsetX: 0,
		offsetY: 5
	});
	//输入完毕后提示是否已存在此商品
	$("#s_item").keyup(function(){
		if($(this).val()!="")
		{
			$.post("checkPro.action",{
				item:$(this).val()
			},function(data){	
				if(data=="false")
				{
					$("#d1").html("*");
				}
				else if(data=="error")
				{
					$("#d1").html("* Check Item Name Timeout");
				}
				else
				{
					$("#d1").html("* This Product has already exists");
				}  					
			});
		}
		else $("#d1").html("*");
	});
	$("#price,#number").each(function(){
		$(this).keyup(function(){
			if($("#price").val()!=""&&$("#number").val()!="")
				$("#cost").html((parseInt($("#price").val())/parseInt($("#number").val())).toString());
		});
	});
	$("#e_price,#e_number").each(function(){
		$(this).keyup(function(){
			if($("#e_price").val()!=""&&$("#e_number").val()!="")
				$("#e_cost").html((parseInt($("#e_price").val())/parseInt($("#e_number").val())).toString());
		});
	});
	$("#addItem").click(function(){
		$("#dialog-form2").dialog( "open" );
  		if ($.browser.msie) {           
  			$("#dialog-form2").css("height",300);    
  		}
	});
	//勾选全部
  	$("#selectAll").click(function(){
  		var checkBool=false;
  		if($(this).attr("checked"))
  			checkBool=true;
  		$("#form2 :checkbox").each(function(){
  			$(this).attr("checked",checkBool);
  		});
  	});
  	$("#deleteAll").click(function(){
  		$(".checkOne:checked").each(function(){
  			var aPos = oTable.fnGetPosition($(this).parent().parent().get(0));
  	  		oTable.fnDeleteRow(aPos);
  		});
  	});
  	var $tr;
  	$("#edit").live("click", function(){
  		$tr=$(this);
 		$("#e_item").val($(this).parent().parent().find("td").eq(1).text());
 		$("#e_date").val($(this).parent().parent().find("td").eq(2).text());
 		$("#e_place").val($(this).parent().parent().find("td").eq(3).text());
 		$("#e_number").val($(this).parent().parent().find("td").eq(4).text());
 		$("#e_price").val($(this).parent().parent().find("td").eq(5).text());
 		$("#e_cost").html((parseInt($("#e_price").val())/parseInt($("#e_number").val())).toString());
  		$("#dialog-form1").dialog( "open" );
  		if ($.browser.msie) {           
  			$("#dialog-form1").css("height",300);    
  		}
  	});
  	$("#delete").live("click", function(){
  		//$(this).parent().parent().remove();
  		var aPos = oTable.fnGetPosition($(this).parent().parent().get(0));
  		oTable.fnDeleteRow(aPos);
  		//alert(oTable.fnGetData(aPos)[1]);
  		//oTable.fnDraw();
  	});
  	var oTable;
	$("#submit").click(function(){
		var boolForm1=true;
		$("#form1 input,#form1 select").each(function(){
			if($(this).val()=="")
				boolForm1=false;
		});
		if(!boolForm1)
		{
			alert("All form files are required");
			return;
		}
		if($("#price").val()!=""&&$("#number").val()!="")
			$("#cost").html((parseInt($("#price").val())/parseInt($("#number").val())).toString());
		$("#table2").append("<tr><td><input class='checkOne' type='checkbox'></input></td><td>"+
				$("#item").val()+"</td><td>"+
				$("#date").val()+"</td><td>"+$("#place").val()+"</td><td>"+
				$("#number").val()+"</td><td>"+$("#price").val()+"</td><td>"+
				"<a href='#' id='edit'>Edit</a>&nbsp&nbsp"+"<a href='#' id='delete'>Delete</a></td></tr>");
		$("#form2").show();
		oTable=$("#table2").dataTable({			
			"bJQueryUI": true,
			"sPaginationType": "full_numbers",
			"bProcessing":true,		
			"bDestroy":true,
			"sDom":'CT<"clear">R<"H"lfr>t<"F"ip>',
			"oTableTools":{
				"sSwfPath":"../../JavaScript/DataTables/extras/TableTools/media/swf/copy_cvs_xls_pdf.swf"
				},
			"oColVis": {
						"buttonText": "Change columns"
					}
		});	
	});	
	$("#save").click(function(){
		var boolAdd=true;
		$("#table2 tr:not([id='title'])").each(function(){
			var trID=$(this).get(0);
			var item = oTable.fnGetData(trID)[1];
			var date=oTable.fnGetData(trID)[2];
			var place=oTable.fnGetData(trID)[3];
			var number=oTable.fnGetData(trID)[4];
			var price=oTable.fnGetData(trID)[5];
			$.post("saveStock.action",{
				item:item,
				date:date,
				place:place,
				number:number,
				price:price
			},function(data){
				if(data=="true")
				{				
			  		oTable.fnDeleteRow(trID);
				}
				else if(data="false")
				{
					boolAdd=false;
					alert("Add the stock information failed");
					return;
				}
				else 
				{
					boolAdd=false;
					alert("Lost connection to the server");
					return;
				}
			});
		});
		if(boolAdd)
			alert("Add the stock information succeed");
	});
	
	//更新对话框	
  	$( "#dialog-form1" ).dialog({
			autoOpen: false,
			height: 450,
			width: 600,
			modal: true,			
			buttons: {
				"Save": function() {
					var boolForm3=true;
					$("#form3 input,#form3 select").each(function(){
						if($(this).val()=="")
							boolForm3=false;
					});
					if(!boolForm3)
					{
						alert("All form files are required");
						return;
					}
					if($("#e_price").val()!=""&&$("#e_number").val()!="")
						$("#e_cost").html((parseInt($("#e_price").val())/parseInt($("#e_number").val())).toString());
					$tr.parent().parent().find("td").eq(1).html($("#e_item").val());
					$tr.parent().parent().find("td").eq(3).html($("#e_date").val());
					$tr.parent().parent().find("td").eq(4).html($("#e_place").val());
					$tr.parent().parent().find("td").eq(5).html($("#e_number").val());
					$tr.parent().parent().find("td").eq(6).html($("#e_price").val());
					$( this ).dialog( "close" );
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});
  	
  	//添加新商品
  	$( "#dialog-form2" ).dialog({
			autoOpen: false,
			height: 450,
			width: 600,
			modal: true,			
			buttons: {
				"Save": function() {
					var $dialog4=$(this);
					//判断必填数据是否为空
	    			if($("#s_item").val()==""||$("#s_category").val()=="")
	    			{
	    				$("#d1").html("* Item Name are required!");
	    				$("#item").select();	
	    			}
	    			else
	    			{
	    				//添加商品信息到数据
	    				$("#table4 tr td input").each(function(){
	    					if($(this).val()=="")
	    						$(this).val("0");
	    				});
	    				$.post("addProduct.action",$("#form4").serialize(),function(data){
	    				if(data=="true")
	    				{
	    					$("#item,#e_item").append("<option>"+$("#s_item").val()+"</option>");
	    					alert("Add New Product Success");
	    					$dialog4.dialog( "close" );
	    				}
	    				else if(data=="false")
	    				{
	    					alert("Add Product failed");
	    				}
	    				else
	    				{
	    					alert("Lost Connection to Database,Please try again later");
	    				}
	    				});				
	    			}
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});
});